﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using btnet;
using System.Collections;

/// <summary>
/// Summary description for backup_db
/// </summary>
public partial class backup_db : System.Web.UI.Page
{
    protected Security security;

    protected string app_data_folder;

    ///////////////////////////////////////////////////////////////////////
    protected void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String footer_label = null;
            if (Application["custom_footer"] != null)
            {
                footer_label = (String)Application["custom_footer"];
            }
            custom_footer_label.Text = footer_label;
        }

        Util.do_not_cache(Response);

        security = new Security();
        security.check_security(HttpContext.Current, Security.MUST_BE_ADMIN);

        titl.InnerText = Util.get_setting("AppTitle", "BugTracker.NET") + " - "
            + "backup db";

        app_data_folder = HttpContext.Current.Server.MapPath(null);
        app_data_folder += "\\App_Data\\";

        if (!IsPostBack)
        {
            get_files();
        }

    }

    protected void get_files()
    {
        string[] backup_files = System.IO.Directory.GetFiles(app_data_folder, "*.bak");

        if (backup_files.Length == 0)
        {
            MyDataGrid.Visible = false;
            return;
        }

        MyDataGrid.Visible = true;

        // sort the files
        ArrayList list = new ArrayList();
        list.AddRange(backup_files);
        list.Sort();

        DataTable dt = new DataTable();
        DataRow dr;

        dt.Columns.Add(new DataColumn("file", typeof(String)));
        dt.Columns.Add(new DataColumn("url", typeof(String)));

        for (int i = 0; i < list.Count; i++)
        {
            dr = dt.NewRow();

            string just_file = System.IO.Path.GetFileName((string)list[i]);
            dr[0] = just_file;
            dr[1] = "download_file.aspx?which=backup&filename=" + just_file;

            dt.Rows.Add(dr);
        }

        DataView dv = new DataView(dt);

        MyDataGrid.DataSource = dv;
        MyDataGrid.DataBind();
    }


    protected void on_backup(Object sender, EventArgs e)
    {
        string date = DateTime.Now.ToString("yyyyMMdd_HHmmss");
        string db = (string)btnet.DbUtil.execute_scalar("select db_name()");
        string backup_file = app_data_folder + "db_backup_" + date + ".bak";
        string sql = "backup database " + db + " to disk = '" + backup_file + "'";
        btnet.DbUtil.execute_nonquery(sql);
        get_files();
    }


    protected void my_button_click(object sender, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "dlt")
        {
            int i = e.Item.ItemIndex;
            string file = MyDataGrid.Items[i].Cells[0].Text;
            System.IO.File.Delete(app_data_folder + file);
            get_files();
        }
    }
}
